<?php

class BrosurController extends BaseController {

	//brosur
	public function getBrosur()
	{
		Session::forget('list');
		Session::forget('filter');
		Session::forget('filterby');
		Session::forget('keyword');
		$brosurs = DB::table('ref_brochure')
							->join('ref_papertype','papertype_id','=','brochure_papertype_id')
							->join('ref_size','size_id','=','brochure_size_id')
							->leftjoin('ref_size_brochure','size_brochure_size_id','=','size_id')
							->leftjoin('ref_size_flayer','size_flayer_size_id','=','size_id')
							->leftjoin('ref_size_envelope','size_envelope_size_id','=','size_id')
							->leftjoin('ref_size_letterhead','size_letterhead_size_id','=','size_id')
							->where('brochure_isdelete','=',0)
							->paginate(10);
		$papers = DB::table('ref_papertype')
							->where('papertype_type','=','brochure')
							->get();
		return View::make('admin.brosur.brosur',compact('brosurs',$brosurs,'papers',$papers));
	}

	public function postBrosur()
	{
		if(Input::get('filter')=='type'){
			Session::forget('name');
			Session::put('filter','type');
			Session::put('list', Input::get('typeby'));
			if(Input::get('typeby')=='all'){
				$brosurs = DB::table('ref_brochure')
				->join('ref_papertype','papertype_id','=','brochure_papertype_id')
				->join('ref_size','size_id','=','brochure_size_id')
				->leftjoin('ref_size_brochure','size_brochure_size_id','=','size_id')
				->leftjoin('ref_size_flayer','size_flayer_size_id','=','size_id')
				->leftjoin('ref_size_envelope','size_envelope_size_id','=','size_id')
				->leftjoin('ref_size_letterhead','size_letterhead_size_id','=','size_id')
				->where('brochure_isdelete','=',0)
				->paginate(10);
			}else{
				$brosurs = DB::table('ref_brochure')
				->join('ref_papertype','papertype_id','=','brochure_papertype_id')
				->join('ref_size','size_id','=','brochure_size_id')
				->leftjoin('ref_size_brochure','size_brochure_size_id','=','size_id')
				->leftjoin('ref_size_flayer','size_flayer_size_id','=','size_id')
				->leftjoin('ref_size_envelope','size_envelope_size_id','=','size_id')
				->leftjoin('ref_size_letterhead','size_letterhead_size_id','=','size_id')
				->where('brochure_isdelete','=',0)
				->where('brochure_type','=',Input::get('typeby'))
				->paginate(10);
			}
		}else if(Input::get('filter')=='keyword'){
			Session::forget('list');
			Session::put('filter','keyword');
			Session::put('name',Input::get('name'));
			$brosurs = DB::table('ref_brochure')
			->join('ref_papertype','papertype_id','=','brochure_papertype_id')
			->join('ref_size','size_id','=','brochure_size_id')
			->leftjoin('ref_size_brochure','size_brochure_size_id','=','size_id')
			->leftjoin('ref_size_flayer','size_flayer_size_id','=','size_id')
			->leftjoin('ref_size_envelope','size_envelope_size_id','=','size_id')
			->leftjoin('ref_size_letterhead','size_letterhead_size_id','=','size_id')
			->where('brochure_isdelete','=',0)
			->where('papertype_name','LIKE','%'.Input::get('name').'%')
			->paginate(10);	
		}else{
			if(Session::get('filter')=="type"){
						if(Input::get('typeby')=='all'){
							$brosurs = DB::table('ref_brochure')
							->join('ref_papertype','papertype_id','=','brochure_papertype_id')
							->join('ref_size','size_id','=','brochure_size_id')
							->leftjoin('ref_size_brochure','size_brochure_size_id','=','size_id')
							->leftjoin('ref_size_flayer','size_flayer_size_id','=','size_id')
							->leftjoin('ref_size_envelope','size_envelope_size_id','=','size_id')
							->leftjoin('ref_size_letterhead','size_letterhead_size_id','=','size_id')
							->where('brochure_isdelete','=',0)
							->paginate(10);
						}else{
							$brosurs = DB::table('ref_brochure')
							->join('ref_papertype','papertype_id','=','brochure_papertype_id')
							->join('ref_size','size_id','=','brochure_size_id')
							->leftjoin('ref_size_brochure','size_brochure_size_id','=','size_id')
							->leftjoin('ref_size_flayer','size_flayer_size_id','=','size_id')
							->leftjoin('ref_size_envelope','size_envelope_size_id','=','size_id')
							->leftjoin('ref_size_letterhead','size_letterhead_size_id','=','size_id')
							->where('brochure_isdelete','=',0)
							->where('brochure_type','=',Input::get('typeby'))
							->paginate(10);
						}
			}else if(Session::get('filter')=="keyword"){
					$brosurs = DB::table('ref_brochure')
						->join('ref_papertype','papertype_id','=','brochure_papertype_id')
						->join('ref_size','size_id','=','brochure_size_id')
						->leftjoin('ref_size_brochure','size_brochure_size_id','=','size_id')
						->leftjoin('ref_size_flayer','size_flayer_size_id','=','size_id')
						->leftjoin('ref_size_envelope','size_envelope_size_id','=','size_id')
						->leftjoin('ref_size_letterhead','size_letterhead_size_id','=','size_id')
						->where('brochure_isdelete','=',0)
						->where('papertype_name','LIKE','%'.Input::get('name').'%')
						->paginate(10);	
			}
		}

		$papers = DB::table('ref_papertype')
							->where('papertype_type','=','brochure')
							->get();
		return View::make('admin.brosur.brosur')->with('brosurs',$brosurs,'papers',$papers);
	}


//--------------------------------------------------------//
	public function postAddbrosur()
	{
		$addbrosur = refbrosur::create(Input::all());

		$log = New adminlog();
		$log->admin_log_admin_id = Session::get('admin_id');	
		$log->admin_log_activity = "Add data brosur(Brosur)";	
		$log->admin_log_date = date('Y-m-d H:i:s');	
		$log->save();
		return Redirect::to('/brosur')->with('message','Data brosur berhasil ditambahkan');
	}

	public function postAddkopsurat()
	{
		$addkopsurat = refbrosur::create(Input::all());

		$log = New adminlog();
		$log->admin_log_admin_id = Session::get('admin_id');	
		$log->admin_log_activity = "Add data brosur(Kop surat)";	
		$log->admin_log_date = date('Y-m-d H:i:s');	
		$log->save();
		return Redirect::to('/brosur')->with('message','Data kop surat berhasil ditambahkan');
	}

	public function postAddflayer()
	{	
		$size = DB::table('ref_size_flayer')
					->join('ref_size','size_id','=','size_flayer_size_id')
					->where('size_flayer_length',Input::get('size_flayer_length'))
					->where('size_flayer_width',Input::get('size_flayer_width'))
					->count();
		$sizeexist = DB::table('ref_size_flayer')
					->join('ref_size','size_id','=','size_flayer_size_id')
					->where('size_flayer_length',Input::get('size_flayer_length'))
					->where('size_flayer_width',Input::get('size_flayer_width'))
					->first();
		if($size==0){

		$newsize = New  refsize();
		$newsize->size_type = 'flayer';
		$newsize>save();

		$flayersize = New refflayer();
		$flayersize->size_flayer_size_id = $newsize->size_id;
		$flayersize->size_flayer_length = Input::get('size_flayer_length');
		$flayersize->size_flayer_width = Input::get('size_flayer_width');
		$flayersize->size_flayer_unit =  'Cm';
		$flayersize->save();

		$addflayer = New refbrosur();
		$addflayer->brochure_papertype_id = Input::get('brochure_papertype_id');
		$addflayer->brochure_type = Input::get('brochure_type');
		$addflayer->brochure_color = Input::get('brochure_color');
		$addflayer->brochure_qty_print_page = Input::get('brochure_qty_price');
		$addflayer->brochure_price = Input::get('brochure_price');
		$addflayer->brochure_qty_price = Input::get('brochure_qty_price');
		$addflayer->brochure_unit = Input::get('brochure_unit');
		$addflayer->brochure_size_id = $newsize->size_id;
		$addflayer->save();
		}else if($size<>0){
			$addflayer = New refbrosur();
			$addflayer->brochure_papertype_id = Input::get('brochure_papertype_id');
			$addflayer->brochure_type = Input::get('brochure_type');
			$addflayer->brochure_color = Input::get('brochure_color');
			$addflayer->brochure_qty_print_page = Input::get('brochure_qty_price');
			$addflayer->brochure_price = Input::get('brochure_price');
			$addflayer->brochure_qty_price = Input::get('brochure_qty_price');
			$addflayer->brochure_unit = Input::get('brochure_unit');
			$addflayer->brochure_size_id = $sizeexist->size_id;
			$addflayer->save();
		}

		$log = New adminlog();
		$log->admin_log_admin_id = Session::get('admin_id');	
		$log->admin_log_activity = "Add data brosur(Flayer)";	
		$log->admin_log_date = date('Y-m-d H:i:s');	
		$log->save();
		return Redirect::to('/brosur')->with('message','Data flayer berhasil ditambahkan');
	}

	public function postAddenvelope()
	{

		$size = DB::table('ref_size_envelope')
					->join('ref_size','size_id','=','size_envelope_size_id')
					->where('size_envelope_length',Input::get('size_envelope_length'))
					->where('size_envelope_width',Input::get('size_envelope_width'))
					->count();
		$sizeexist = DB::table('ref_size_envelope')
					->join('ref_size','size_id','=','size_envelope_size_id')
					->where('size_envelope_length',Input::get('size_envelope_length'))
					->where('size_envelope_width',Input::get('size_envelope_width'))
					->first();
		if($size==0){
		$newsize = New refsize();
		$newsize->size_type = 'envelope';
		$newsize->save();

		$envelopesize = New refenvelope();
		$envelopesize->size_envelope_size_id = $newsize->size_id;
		$envelopesize->size_envelope_length = Input::get('size_envelope_length');
		$envelopesize->size_envelope_width = Input::get('size_envelope_width');
		$envelopesize->size_envelope_unit =  'Cm';
		$envelopesize->save();

		$addenvelope = New refbrosur();
		$addenvelope->brochure_papertype_id = Input::get('brochure_papertype_id');
		$addenvelope->brochure_type = Input::get('brochure_type');
		$addenvelope->brochure_color = Input::get('brochure_color');
		$addenvelope->brochure_qty_print_page = Input::get('brochure_qty_price');
		$addenvelope->brochure_price = Input::get('brochure_price');
		$addenvelope->brochure_qty_price = Input::get('brochure_qty_price');
		$addenvelope->brochure_unit = Input::get('brochure_unit');
		$addenvelope->brochure_size_id = $newsize->size_id;
		$addenvelope->save();
		}else if($size<>0){
			$addenvelope = New refbrosur();
			$addenvelope->brochure_papertype_id = Input::get('brochure_papertype_id');
			$addenvelope->brochure_type = Input::get('brochure_type');
			$addenvelope->brochure_color = Input::get('brochure_color');
			$addenvelope->brochure_qty_print_page = Input::get('brochure_qty_price');
			$addenvelope->brochure_price = Input::get('brochure_price');
			$addenvelope->brochure_qty_price = Input::get('brochure_qty_price');
			$addenvelope->brochure_unit = Input::get('brochure_unit');
			$addenvelope->brochure_size_id = $sizeexist->size_id;
			$addenvelope->save();

		}

		$log = New adminlog();
		$log->admin_log_admin_id = Session::get('admin_id');	
		$log->admin_log_activity = "Add data brosur(Amplop)";	
		$log->admin_log_date = date('Y-m-d H:i:s');	
		$log->save();
		return Redirect::to('/brosur')->with('message','Data flayer berhasil ditambahkan');
	}
//-----------------------------------------------------------//
	public function postEditbrosur($id)
	{
		$editbrosur = refbrosur::find($id);
		$editbrosur->brochure_papertype_id = Input::get('brochure_papertype_id');
		$editbrosur->brochure_color = Input::get('brochure_color');
		$editbrosur->brochure_qty_print_page = Input::get('brochure_qty_print_page');
		$editbrosur->brochure_price = Input::get('brochure_price');
		$editbrosur->brochure_qty_price = Input::get('brochure_qty_price');
		$editbrosur->brochure_unit = Input::get('brochure_unit');
		$editbrosur->brochure_size_id = Input::get('brochure_size_id');
		$editbrosur->save();

		$log = New adminlog();
		$log->admin_log_admin_id = Session::get('admin_id');	
		$log->admin_log_activity = "Edit data brosur";	
		$log->admin_log_date = date('Y-m-d H:i:s');	
		$log->save();
		return Redirect::to('/brosur')->with('message','Data flayer berhasil ditambahkan');
	}

	public function postEditletterhead($id)
	{
		$editletterhead = refbrosur::find($id);
		$editletterhead->brochure_papertype_id = Input::get('brochure_papertype_id');
		$editletterhead->brochure_color = Input::get('brochure_color');
		$editletterhead->brochure_qty_print_page = Input::get('brochure_qty_print_page');
		$editletterhead->brochure_price = Input::get('brochure_price');
		$editletterhead->brochure_qty_price = Input::get('brochure_qty_price');
		$editletterhead->brochure_unit = Input::get('brochure_unit');
		$editletterhead->brochure_size_id = Input::get('brochure_size_id');
		$editletterhead->save();

		$log = New adminlog();
		$log->admin_log_admin_id = Session::get('admin_id');	
		$log->admin_log_activity = "Edit data brosur(Kop surat)";	
		$log->admin_log_date = date('Y-m-d H:i:s');	
		$log->save();
		return Redirect::to('/brosur')->with('message','Data flayer berhasil ditambahkan');
	}

	public function postEditflayer($id)
	{
		$editflayer = refbrosur::find($id);
		$editflayer->brochure_papertype_id = Input::get('brochure_papertype_id');
		$editflayer->brochure_color = Input::get('brochure_color');
		$editflayer->brochure_qty_print_page = Input::get('brochure_qty_print_page');
		$editflayer->brochure_price = Input::get('brochure_price');
		$editflayer->brochure_qty_price = Input::get('brochure_qty_price');
		$editflayer->brochure_unit = Input::get('brochure_unit');

		$flayersize = refflayer::find($editflayer->brochure_size_id);
		$flayersize->size_flayer_length = Input::get('size_flayer_length');
		$flayersize->size_flayer_width = Input::get('size_flayer_width');
		$flayersize->size_flayer_unit =  'Cm';
		$flayersize->save();

		$editflayer->save();

		$log = New adminlog();
		$log->admin_log_admin_id = Session::get('admin_id');	
		$log->admin_log_activity = "Edit data brosur(Flayer)";	
		$log->admin_log_date = date('Y-m-d H:i:s');	
		$log->save();
		return Redirect::to('/brosur')->with('message','Data flayer berhasil ditambahkan');
	}

		public function postEditenvelope($id)
	{
		$editenvelope = refbrosur::find($id);
		$editenvelope->brochure_papertype_id = Input::get('brochure_papertype_id');
		$editenvelope->brochure_color = Input::get('brochure_color');
		$editenvelope->brochure_qty_print_page = Input::get('brochure_qty_print_page');
		$editenvelope->brochure_price = Input::get('brochure_price');
		$editenvelope->brochure_qty_price = Input::get('brochure_qty_price');
		$editenvelope->brochure_unit = Input::get('brochure_unit');

		$envelopesize = refenvelope::find($editenvelope->brochure_size_id);
		$envelopesize->size_envelope_length = Input::get('size_envelope_length');
		$envelopesize->size_envelope_width = Input::get('size_envelope_width');
		$envelopesize->size_envelope_unit =  'Cm';
		$envelopesize->save();

		$editenvelope->save();

		$log = New adminlog();
		$log->admin_log_admin_id = Session::get('admin_id');	
		$log->admin_log_activity = "Edit data brosur(Amplop)";	
		$log->admin_log_date = date('Y-m-d H:i:s');	
		$log->save();
		return Redirect::to('/brosur')->with('message','Data flayer berhasil ditambahkan');
	}


	








	public function postDeletebrosur()
	{
		$brosur = DB::table('ref_brochure')
				->get();
		foreach($brosur as $brosur){
		if(Input::get($brosur->brochure_id)=='1'){
			$id = Input::get($brosur->brochure_id);
			$brosur = DB::table('ref_brochure')
					->where('brochure_id','=',$brosur->brochure_id)
					->update(array('brochure_isdelete' => 1));

			$log = New adminlog();
			$log->admin_log_admin_id = Session::get('admin_id');	
			$log->admin_log_activity = "Delete data brosur";	
			$log->admin_log_date = date('Y-m-d H:i:s');	
			$log->save();
		}
		}
		return Redirect::to('/brosur')->with('message','Data brosur berhasil dihapus');
	}



}